<template>
  <div class="directorys-container">
    <!-- 卡片区 -->
    <el-card>
      <div slot="header" v-if="this.tagsId">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subjectName }}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <el-row type="flex" justify="space-between">
        <el-col :span="18">
          <el-form :inline="true" :model="requestParameters" ref="clearRormRef">
            <el-form-item label="标签名称" prop="tagName" label-width="80px">
              <el-input
                size="small"
                v-model.trim="requestParameters.tagName"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="state" label-width="80px">
              <el-select v-model="requestParameters.state" placeholder="请选择">
                <el-option
                  v-for="(item, index) in options"
                  :value="item"
                  :key="item.key"
                  :label="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="onclear">清除</el-button>
              <el-button size="small" type="primary" @click="getList"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>

        <el-col :span="4">
          <el-link
            v-if="this.tagsId"
            class="directorys-link"
            type="primary"
            size="small"
            @click="$router.back()"
            :underline="false"
            icon="el-icon-arrow-left"
            >返回学科</el-link
          >

          <el-button
            class="subjects-btn"
            type="success"
            size="small"
            @click="addDirectorysList"
          >
            <i class="el-icon-edit"></i>
            新增标签
          </el-button>
        </el-col>
      </el-row>

      <!-- 提示的文案区 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- 表格区 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"></el-table-column>
        <el-table-column prop="username" label="创建者"></el-table-column>
        <el-table-column label="创建日期 ">
          <template slot-scope="{ row }">
            {{ row.addDate | parseTimeByString('{y}-{m}-{d} {h}:{i}:{s}') }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="{ row }">
            {{ row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-link
              :underline="false"
              type="primary"
              size="mini"
              @click="changeState(row.id, row.state)"
              >{{ row.state === 1 ? '禁用' : '启用' }}</el-link
            >

            <el-link
              :disabled="row.state === 1"
              :underline="false"
              :type="row.state === 1 ? 'info' : 'primary'"
              size="mini"
              @click="updateDir(row.id)"
              >修改</el-link
            >
            <el-link
              :disabled="row.state === 1"
              :underline="false"
              :type="row.state === 1 ? 'info' : 'primary'"
              size="mini"
              @click="deleteDirectorys(row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination" v-if="!this.updataId">
        <PageTool
          :paginationPage="requestParameters.page"
          :paginationPagesize="requestParameters.pagesize"
          :total="total"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        ></PageTool>
      </div>
    </el-card>

    <!-- 新增修改弹窗 -->
    <tags-add
      ref="updateRef"
      :directorysVisible.sync="directorysVisible"
      :updataId.sync="updataId"
      @directorysAdd="directorysAdd"
      :tagsId="tagsId"
    />
  </div>
</template>
<script>
import { list, remove, changeState } from '@/api/hmmm/tags'

import PageTool from '@/module-manage/components/page-tool'
import TagsAdd from '../components/tags-add'

export default {
  name: '',
  components: {
    PageTool,
    TagsAdd
  },
  props: {},
  data() {
    return {
      tableData: [],
      total: 0,
      requestParameters: {
        page: 1,
        pagesize: 10,
        tagName: ''
      },
      options: { 已启用: 1, 已禁用: 0 },
      directorysVisible: false,

      updataId: null,
      tagsId: null,
      subjectName: null
    }
  },
  computed: {},
  watch: {},
  methods: {
    directorysAdd() {
      this.getList()
    },

    geTagsId() {
      const directorysparams = this.$route.params
      this.tagsId = directorysparams.id
      this.subjectName = directorysparams.subjectName
      // console.log(directorysparams)
    },

    async getList() {
      try {
        let res
        if (this.tagsId) {
          this.requestParameters.subjectID = this.tagsId

          res = await list(this.requestParameters)
        } else {
          res = await list(this.requestParameters)
          // console.log(res)
        }
        this.tableData = res.data.items
        this.total = res.data.counts
      } catch (error) {
        this.$message.error('获取数据失败')
      }
    },
    // 每页显示条数
    handleSizeChange(newsize) {
      this.requestParameters.pagesize = newsize
      if (this.requestParameters.page === 1) {
        this.getList(this.requestParameters)
      }
    },
    // 当前页码
    handleCurrentChange(newpage) {
      this.requestParameters.page = newpage
      this.getList()
    },

    onclear() {
      this.$refs.clearRormRef.resetFields()
      this.getList()
    },

    addDirectorysList() {
      this.directorysVisible = true
    },

    // 学科修改
    async updateDir(id) {
      this.updataId = id
      this.directorysVisible = true
      await this.$refs.updateRef.updateSubject(id)
    },

    // 标签状态
    async changeState(id, state) {
      if (state === 1) {
        state = 0
      } else {
        state = 1
      }
      const change = { id, state }
      try {
        await changeState(change)
        // console.log(res)
        const index = this.tableData.findIndex(val => {
          return (val.id = id)
        })
        this.tableData[index].state = state
        this.getList()
      } catch (error) {
        this.$message.error('获取数据失败')
      }
    },
    // 标签删除

    async deleteDirectorys(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除标签, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      try {
        await remove(id)
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        this.$message.info('您取消了删除')
      }
    }
  },
  created() {},
  mounted() {
    this.geTagsId()
    this.getList()
  }
}
</script>
<style scoped lang="scss">
.directorys-container {
  padding: 10px;
  .el-card__body {
    padding: unset;
  }
  .breadcrumb-box {
    padding: 20px;
  }

  .subjects-btn {
    float: right;
  }
  .el-table {
    margin-top: 15px;
  }
  .pagination {
    margin-top: 10px;
    text-align: right;
  }
  .el-link {
    margin: 4px;
  }
  .directorys-link {
    margin: 10px 0 0 10px;
  }
}
</style>
